package com.ids.idtma;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * Created by Administrator on 2016/4/19 0019.
 * 继承类控件,为了改变控件功能
 */
public class MyListView extends ListView implements GestureDetector.OnGestureListener, View.OnTouchListener {

    private GestureDetector mDetector;
    //滑动出现的删除按钮
    private View mBtnDel;
    //ListView中的某一项
    private ViewGroup mListItem;
    //用来保存选中对应的索引
    private int mSelectIndex;
    //是否已经显示了对应的按钮
    private boolean isDelShow;

    public MyListView(Context context) {
        this(context, null);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //创建手势识别器对象 6个方法
        mDetector = new GestureDetector(context, this);
        //创建触碰事件监听
        this.setOnTouchListener(this);

    }

    //触碰处理方法
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        //获取到点击列表项目的索引,zai ListView中
        mSelectIndex = pointToPosition((int) event.getX(), (int) event.getY());

        //删除以显示的按钮
        if (isDelShow) {
            //删除按钮
            //使用动画移除已经显示的删除按钮
            btnHide(mBtnDel);
            //获取到对应要删除的列表项,进行删除列表中显示删除按钮
            mListItem.removeView(mBtnDel);
            mBtnDel = null;
            isDelShow = false;

        } else {

            //把MotionEvent交给手势识别器来处理
            mDetector.onTouchEvent(event);
        }

        return false;
    }


    //手势识别器监听需要实现的方法

    /**
     * @param e1        开始时监听事件对象
     * @param e2        抛掷时(结束滑动)监听时间对象
     * @param velocityX 在x轴移动的速率 (e2.getX() -e1.getX()) / time
     * @param velocityY 在y轴上移动的速率(e2.getY() -e1.getY()) / time
     * @return 抛掷动作
     */
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //判断往左边滑动,并且木有显示删除按钮
        if (!isDelShow &&
                Math.abs(velocityX) > Math.abs(velocityY)
                &&velocityX<0) {

            //获取选择的项
            mListItem = (ViewGroup) getChildAt(mSelectIndex - getFirstVisiblePosition());

            //处理选择这个选择列表的删除按钮
            mBtnDel = LayoutInflater.from(getContext()).inflate(R.layout.del_btn, null);
            //点击按钮时,要删除选择的项
            mBtnDel.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    //处理删除
                    //把删除按钮先移除
                    btnHide(v);

                    mListItem.removeView(mBtnDel);
                    mBtnDel = null;
                    isDelShow = false;
                    //调用回调的监听对象的删除方法,在Activity的删除方法来移除对应项
                    listener.onDeleteItem(mSelectIndex);
                    //removeView(mListItem); //ListView它的移除方法
                }
            });
            //创建删除按钮的布局参数
            RelativeLayout.LayoutParams layoutParams =
                    new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置删除按钮显示的位置靠右并且垂直居中
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
            mBtnDel.setLayoutParams(layoutParams);
            //把删除按钮加到对应选择的列表项
            mListItem.addView(mBtnDel);
            btnShow(mBtnDel);
            isDelShow = true;

        }

        return false;
    }

    //设置外部删除列表项的监听
    public interface OnItemDelListener {
        public void onDeleteItem(int selectIndex);
    }

    private OnItemDelListener listener;

    public void setOnItemDelListener(OnItemDelListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        //没有显示删除按钮,就不做任何处理
        if (!isDelShow) {
            mSelectIndex = pointToPosition((int) e.getX(), (int) e.getY());
        }
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }


    //按钮显示时的动画
    private void btnShow(View v) {
        v.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.animi_btn_show));
    }

    //按钮隐藏时的动画
    private void btnHide(View v) {
        v.startAnimation(
                AnimationUtils.loadAnimation(getContext(), R.anim.animi_btn_hide));
    }

}